package com.android.webserver.manager;

import com.android.webserver.R;
import com.android.webserver.db.DatabaseOpenHelper;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * @author Baptiste GOURDIN
 *
 */
public class WebApps extends ListActivity
{
	Cursor							c;
	DatabaseOpenHelper	db;
	CursorAdapter				adapter;

	class WebAppAdapter extends CursorAdapter
	{
		public WebAppAdapter(Context context, Cursor cursor)
		{
			super(context, cursor);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor)
		{

			String name = c.getString(1);
			String path = c.getString(2);
			boolean enabled = (c.getInt(3) != 0);

			((TextView) view.findViewById(R.id.title)).setText(name);
			((TextView) view.findViewById(R.id.path)).setText("Path : /" + path);
			if (enabled)
				((TextView) view.findViewById(R.id.status)).setText("Status : Active");
			else
				((TextView) view.findViewById(R.id.status))
						.setText("Status : Unactive");
			view.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					startActivity(new Intent(WebApps.this, WebAppDetail.class));
				}
			});
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent)
		{
			LayoutInflater inflater = LayoutInflater.from(context);
			View v = inflater.inflate(R.layout.webappsitem, parent, false);
			return v;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		db = new DatabaseOpenHelper(this);
		c = db.getApplications();
		startManagingCursor(c);
		adapter = new WebAppAdapter(this, c);
		setListAdapter(adapter);
	}

}
